package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.BrandStore;
import cn.szzsi.common.model.BrandStoreCategory;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

import java.util.List;

/**
 * 品牌门店分类
 * Created by zhengy on 2017/3/21.
 */
public class BrandStoreCategoryController extends BaseAdminController<BrandStoreCategory>{
    private String shopId;
    private BrandStoreCategory brandStoreCategory;
    public void list(){
        render(CommonUtil.TEMPLATE_PATH+"/business/brandstorecategory/brand_store_category_list.html");
    }
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<BrandStoreCategory> lbsc = BrandStoreCategory.dao.find("select * from mall_brand_store_category where shopId = ? order by createDate desc",shopId);
        for(BrandStoreCategory bsc:lbsc){
            bsc.init();
        }
        renderJson(lbsc);
    }

    public void add(){
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        render(CommonUtil.TEMPLATE_PATH+"/business/brandstorecategory/brand_store_category_input.html");
    }
    public void save(){
        shopId=getAttr("shopId");
        brandStoreCategory = getModel(BrandStoreCategory.class);
        brandStoreCategory.set("shopId",shopId);
        if(BrandStoreCategory.dao.getByName(shopId,brandStoreCategory.getName())!=null){
            renderJson(Ret.fail("info","添加失败,门店类别名称已存在!"));
            return;
        }
        saved(brandStoreCategory);
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }
    public void edit(){
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("shopId",shopId);
            setAttr("brandStoreCategory", BrandStoreCategory.dao.findById(id));
        }
        render(CommonUtil.TEMPLATE_PATH+"/business/brandstorecategory/brand_store_category_input.html");
    }
    public void update(){
        shopId=getAttr("shopId");
        brandStoreCategory = getModel(BrandStoreCategory.class);
        updated(brandStoreCategory);
        if(BrandStoreCategory.dao.getByNameAndId(shopId,brandStoreCategory.getName(),brandStoreCategory.getId())!=null){
            renderJson(Ret.fail("info","修改失败,门店类别名称已存在!"));
            return;
        }
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
    }

    public void delete(){
        ids = getParaValues("ids");
        for(String id:ids){
            if(BrandStore.dao.getByStoreCategoryId(id).size()>0){
                renderJson(Ret.fail("info","删除失败,已经有门店绑定该门店类别!"));
                return;
            }
            BrandStoreCategory.dao.deleteById(id);
        }
        renderJson(Ret.ok("info","删除成功!"));
    }
}
